package com.ofek.eventos;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ofek.applicationbase.broadcast.Delegate;
import com.ofek.applicationbase.forms.FieldQConnector;
import com.ofek.applicationbase.forms.QuestionView;
import com.ofek.applicationbase.forms.question.ConditionalQuestionView;
import com.ofek.applicationbase.forms.question.OptionBoxQuestionView;
import com.ofek.applicationbase.forms.question.QuestionOption;
import com.ofek.applicationbase.forms.question.TimeQuestionView;
import com.ofek.eventos.db.datatypes.helptypes.ScheduleFieldInEvent;

public class ScheduleQuestionView extends QuestionView {

	protected static final long HOUR = 60*1000*60;
	private ScheduleFieldInEvent schedule;
	private final ArrayList<FieldQConnector> connectors = new ArrayList<FieldQConnector>();
	private final ArrayList<FieldQConnector> connectorsThis = new ArrayList<FieldQConnector>();
	private final TimeQuestionView from;
	private final ConditionalQuestionView conditionalIndefinite;
	private final LinearLayout findViewById;
	private final android.widget.LinearLayout.LayoutParams layoutParams;
	private final TimeQuestionView to;
	private final ConditionalQuestionView conditionalEventRepeat;
	private final OptionBoxQuestionView repeatOptionBox;
	private final TimeQuestionView reaptUntill;
	private final ConditionalQuestionView conditionalRepeatUntill;
	public ScheduleQuestionView(final Activity context) throws Exception {
		super(R.layout.question_schedule, context);

		findViewById = (LinearLayout) findViewById(R.id.container);
		layoutParams = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

		from = new TimeQuestionView(R.string.from,context);
		add(from);

		conditionalIndefinite = new ConditionalQuestionView(R.string.indefinite, context, false);
		add(conditionalIndefinite);

		to = new TimeQuestionView(R.string.to,context);
		add(to);
		conditionalIndefinite.addConditionalQuestion(to);

		conditionalEventRepeat = new ConditionalQuestionView(R.string.event_repeat, context);
		add(conditionalEventRepeat);

		repeatOptionBox = new OptionBoxQuestionView(
				R.string.event_repeat_every, context,
				new TimeOption(context.getString(R.string.daily),
						Constants.DAILY), new TimeOption(
								context.getString(R.string.weekly),
								Constants.WEEKLY), new TimeOption(
										context.getString(R.string.monthly),
										Constants.MONTHLY), new TimeOption(
												context.getString(R.string.yearly),
												Constants.YEARLY), new TimeOption(
														context.getString(R.string.custom), -1) {
					@Override
					public void onChosen() {
						Toast.makeText(context,
								"Not implemented yet",
								Toast.LENGTH_LONG).show();
					}
				});
		conditionalEventRepeat.addConditionalQuestion(repeatOptionBox);
		add(repeatOptionBox);

		conditionalRepeatUntill = new ConditionalQuestionView(R.string.repeat_untill, context,false);
		add(conditionalRepeatUntill);

		reaptUntill = new TimeQuestionView(R.string.untill,context);
		add(reaptUntill);


		conditionalRepeatUntill.addConditionalQuestion(reaptUntill);
		conditionalEventRepeat.addConditionalQuestion(reaptUntill);
		conditionalEventRepeat.addConditionalQuestion(conditionalRepeatUntill);

	}

	private void add(final QuestionView qv) {
		qv.registerAnswerChangedListener(new Delegate<Object>() {

			@Override
			public void invoke(final Object t) {
				schedule.endTime = conditionalIndefinite.getAnswer().equals(true) ? ScheduleFieldInEvent.INDEFENITE : (Long)to.getAnswer();
				schedule.repeatUntil = conditionalEventRepeat.getAnswer().equals(false) || conditionalRepeatUntill.getAnswer().equals(true) ? ScheduleFieldInEvent.INDEFENITE : (Long)reaptUntill.getAnswer();
				schedule.repeatEvery = conditionalEventRepeat.getAnswer().equals(false) ? ScheduleFieldInEvent.NO_REPEAT : (Long)repeatOptionBox.getAnswer();
				schedule.startTime = (Long) from.getAnswer();
				broadcastAnswerChanged(schedule);
			}
		});
		findViewById.addView(qv,layoutParams);
	}

	private class TimeOption extends QuestionOption {
		private final String string;
		private final long time;

		public TimeOption(final String timeString, final long time) {
			string = timeString;
			this.time = time;
		}

		@Override
		public String toString() {
			return string;
		}

		@Override
		public Object toAnswer() {
			return time;
		}

	}
	@Override
	public Serializable getAnswer() {
		return schedule;
	}

	@Override
	public void setAnswer(final Object defaultAnswer) {
		if (defaultAnswer==null) return;
		schedule = (ScheduleFieldInEvent) defaultAnswer;
		reaptUntill.setAnswer(schedule.repeatUntil);
		repeatOptionBox.setAnswer(schedule.repeatEvery);
		from.setAnswer(schedule.startTime);
		to.setAnswer(schedule.endTime);
		conditionalIndefinite.setAnswer(schedule.endTime == ScheduleFieldInEvent.INDEFENITE);
		boolean repeat;
		conditionalEventRepeat.setAnswer(repeat = schedule.repeatEvery != ScheduleFieldInEvent.NO_REPEAT);
		conditionalRepeatUntill.setAnswer( repeat && schedule.repeatUntil == ScheduleFieldInEvent.INDEFENITE);

	}

	@Override
	public void setEditable(final boolean edit) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDefaultAnswer(final Object defaultAnswer, final boolean shouldPrePick) {
		// TODO Auto-generated method stub

	}


}
